import random
import tkinter as tk
from tkinter import messagebox


class MinesweeperGUI:
    def __init__(self, master):
        self.master = master
        self.master.title('扫雷游戏')
        self.width = 15
        self.height = 15
        self.mines = 10*3
        self.buttons = []
        self.board = [[' ' for _ in range(self.width)] for _ in range(self.height)]
        self.mine_positions = []
        self.revealed = [[False for _ in range(self.width)] for _ in range(self.height)]
        self.game_over = False

        # 数字颜色映射
        self.number_colors = {
            1: 'blue',
            2: 'green',
            3: 'red',
            4: 'purple',
            5: 'maroon',
            6: 'turquoise',
            7: 'black',
            8: 'gray'
        }

        # 创建游戏界面
        self.create_board()
        self.place_mines()

    def create_board(self):
        # 创建按钮网格
        for i in range(self.height):
            row = []
            for j in range(self.width):
                button = tk.Button(self.master, width=2, height=1,
                                   command=lambda x=i, y=j: self.click(x, y),
                                   font=('Arial', 12, 'bold'))  # 设置更大的字体
                button.grid(row=i, column=j)
                button.bind('<Button-3>', lambda e, x=i, y=j: self.right_click(x, y))
                row.append(button)
            self.buttons.append(row)

    def place_mines(self):
        positions = [(x, y) for x in range(self.height) for y in range(self.width)]
        self.mine_positions = random.sample(positions, self.mines)
        for x, y in self.mine_positions:
            self.board[x][y] = '*'

    def count_adjacent_mines(self, x, y):
        count = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                new_x, new_y = x + dx, y + dy
                if (0 <= new_x < self.height and
                        0 <= new_y < self.width and
                        (new_x, new_y) in self.mine_positions):
                    count += 1
        return count

    def reveal(self, x, y):
        if not (0 <= x < self.height and 0 <= y < self.width):
            return

        if self.revealed[x][y]:
            return

        self.revealed[x][y] = True
        button = self.buttons[x][y]

        if (x, y) in self.mine_positions:
            button.config(text='💣', bg='red')
            self.game_over = True
            self.show_all_mines()
            messagebox.showinfo("游戏结束", "很遗憾，你踩到地雷了！")
            return

        adjacent_mines = self.count_adjacent_mines(x, y)
        if adjacent_mines == 0:
            button.config(text='', bg='lightgray', relief=tk.SUNKEN)
            for dx in [-1, 0, 1]:
                for dy in [-1, 0, 1]:
                    self.reveal(x + dx, y + dy)
        else:
            # 设置数字颜色
            button.config(
                text=str(adjacent_mines),
                bg='lightgray',
                fg=self.number_colors.get(adjacent_mines, 'black'),
                relief=tk.SUNKEN
            )

        if self.check_win():
            messagebox.showinfo("恭喜", "你赢了！")

    def right_click(self, x, y):
        if not self.revealed[x][y]:
            button = self.buttons[x][y]
            current = button.cget('text')
            if current == '🚩':
                button.config(text='')
            else:
                button.config(text='🚩')
        return 'break'

    def click(self, x, y):
        if not self.game_over and not self.revealed[x][y]:
            self.reveal(x, y)

    def show_all_mines(self):
        for x, y in self.mine_positions:
            self.buttons[x][y].config(text='💣', bg='red')

    def check_win(self):
        for i in range(self.height):
            for j in range(self.width):
                if not self.revealed[i][j] and (i, j) not in self.mine_positions:
                    return False
        return True


def main():
    root = tk.Tk()
    game = MinesweeperGUI(root)
    root.mainloop()


if __name__ == "__main__":
    main()